<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="表情符号" prop="emoji">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.emoji"-->
      <!--          placeholder="请输入表情符号"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="TTS服务ID" prop="ttsId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.ttsId"-->
      <!--          placeholder="请输入TTS服务ID"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="角色语音名称" prop="voiceName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.voiceName"-->
      <!--          placeholder="请输入角色语音名称"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="状态" prop="state">-->
      <!--        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.enable_status"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['business:role:add']"-->
      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['business:role:edit']"-->
<!--        >修改-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:role:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="锦囊Id" align="center" prop="roleId"/>
      <el-table-column label="名称" align="center" prop="roleName"/>
      <el-table-column label="提示词" show-overflow-tooltip align="center" prop="roleDesc"/>
      <!--      <el-table-column label="概述" align="center" prop="info"/>-->
      <el-table-column label="音量" align="center" prop="volume"/>
      <el-table-column label="语速" align="center" prop="speech"/>
      <!--      <el-table-column label="表情符号" align="center" prop="emoji"/>-->
      <!--      <el-table-column label="图片" align="center" prop="image" width="100">-->
      <!--        <template slot-scope="scope">-->
      <!--          <image-preview :src="scope.row.image" :width="50" :height="50"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="视频" align="center" prop="video"/>-->
      <!--      <el-table-column label="滚动词" align="center" prop="rollingWords"/>-->
      <!--      <el-table-column label="欢迎词" align="center" prop="welcomeWords"/>-->
      <el-table-column label="数据集" align="center" prop="documentTypeName"/>
      <!--      <el-table-column label="角色头像" align="center" prop="avatar"/>-->
      <!--      <el-table-column label="TTS服务ID" align="center" prop="ttsId"/>-->
      <!--      <el-table-column label="模型" align="center" prop="modelName"/>-->
      <!--      <el-table-column label="STT服务ID" align="center" prop="sttId"/>-->
      <!--      <el-table-column label="语音检测阈值" align="center" prop="vadSpeechTh"/>-->
      <!--      <el-table-column label="静音检测阈值" align="center" prop="vadSilenceTh"/>-->
      <!--      <el-table-column label="能量检测阈值" align="center" prop="vadEnergyTh"/>-->
      <!--      <el-table-column label="静音检测时间" align="center" prop="vadSilenceMs"/>-->
      <el-table-column label="角色语音名称" align="center" prop="voiceName"/>
      <!--      <el-table-column label="状态" align="center" prop="state">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.enable_status" :value="scope.row.state"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="是否默认角色：1-是，0-否" align="center" prop="isDefault"/>-->
      <el-table-column label="创建人" align="center" prop="nickName"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:role:edit']"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:role:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改锦囊管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="名称" prop="roleName">
          <el-input v-model="form.roleName" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="数据集" prop="knowledgeType">
          <el-select
            v-model="documentTypeList"
            multiple
            placeholder="请选择数据集"
            style="width: 100%">
            <el-option
              v-for="item in typeOptions"
              :key="item.id"
              :label="item.typeName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="提示词" prop="roleDesc">
          <el-input v-model="form.roleDesc" type="textarea" :rows="10" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="概述" prop="info">
          <el-input v-model="form.info" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="音量" prop="volume">
          <el-slider
            v-model="form.volume"
            show-input>
          </el-slider>
        </el-form-item>
        <el-form-item label="语速" prop="speech">
          <el-select v-model="form.speech" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.speech_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="表情符号" prop="emoji">
          <el-select v-model="form.emoji" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.expression_list"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="待机图片" prop="image">
          <image-upload v-model="form.image" :limit="1"/>
        </el-form-item>
        <el-form-item label="视频" prop="video">
          <file-upload v-model="form.video" :file-type="['mp3']" :file-size="50" :limit="1"/>
        </el-form-item>
        <el-form-item label="滚动词" prop="rollingWords">
          <el-input v-model="form.rollingWords" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="欢迎词" prop="welcomeWords">
          <el-input v-model="form.welcomeWords" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <!--        <el-form-item label="角色头像" prop="avatar">-->
        <!--          <el-input v-model="form.avatar" placeholder="请输入角色头像"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="TTS服务ID(文本转语音)" prop="ttsId">-->
        <!--          <el-select v-model="form.ttsId" placeholder="请选择TTS服务" clearable>-->
        <!--            <el-option-->
        <!--              v-for="item in ttsOptions"-->
        <!--              :key="item.configId"-->
        <!--              :label="item.configName"-->
        <!--              :value="item.configId">-->
        <!--            </el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="STT服务ID(语音转文本)" prop="sttId">-->
        <!--          <el-select v-model="form.sttId" placeholder="请选择STT服务" clearable>-->
        <!--            <el-option-->
        <!--              v-for="item in sttOptions"-->
        <!--              :key="item.configId"-->
        <!--              :label="item.configName"-->
        <!--              :value="item.configId">-->
        <!--            </el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->

        <!--        <el-form-item label="语音检测阈值" prop="vadSpeechTh">-->
        <!--          <el-input v-model="form.vadSpeechTh" placeholder="请输入语音检测阈值"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="静音检测阈值" prop="vadSilenceTh">-->
        <!--          <el-input v-model="form.vadSilenceTh" placeholder="请输入静音检测阈值"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="能量检测阈值" prop="vadEnergyTh">-->
        <!--          <el-input v-model="form.vadEnergyTh" placeholder="请输入能量检测阈值"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="静音检测时间" prop="vadSilenceMs">-->
        <!--          <el-input v-model="form.vadSilenceMs" placeholder="请输入静音检测时间"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="音色" prop="voiceName">
          <el-select v-model="form.voiceName" placeholder="请选择音色">
            <el-option
              v-for="dict in dict.type.voice_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="状态" prop="state">-->
        <!--          <el-select v-model="form.state" placeholder="请选择状态">-->
        <!--            <el-option-->
        <!--              v-for="dict in dict.type.enable_status"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.label"-->
        <!--              :value="dict.value"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
<!--        <el-form-item label="创建人" prop="userId">-->
<!--          <el-input v-model="form.userId" placeholder="请输入创建人"/>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listJnRole, getJnRole, delJnRole, addJnRole, updateJnRole} from "@/api/business/jnrole";
import {alllistLllmConfig} from "@/api/business/LllmConfig";
import {alllistType} from "@/api/business/type";


export default {
  name: "jnrole",
  dicts: ['enable_status', 'voice_type','expression_list','speech_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 锦囊管理表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleName: null,
        roleDesc: null,
        info: null,
        volume: null,
        speech: null,
        emoji: null,
        image: null,
        video: null,
        rollingWords: null,
        welcomeWords: null,
        knowledgeType: null,
        avatar: null,
        ttsId: null,
        modelId: null,
        sttId: null,
        vadSpeechTh: null,
        vadSilenceTh: null,
        vadEnergyTh: null,
        vadSilenceMs: null,
        voiceName: null,
        state: null,
        isDefault: null,
        userId: null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        roleName: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ],
        // voiceName: [
        //   {required: true, message: "角色语音名称不能为空", trigger: "blur"}
        // ],
        // userId: [
        //   {required: true, message: "创建人不能为空", trigger: "blur"}
        // ],
      },
      // TTS选项列表
      ttsOptions: [],
      // STT选项列表
      sttOptions: [],
      // 类型选项列表
      typeOptions: [],
      documentTypeList: []
    };
  },
  created() {
    this.getList();
    this.getAllLllmConfigs();
    this.getAllTypes();
  },
  methods: {
    /** 查询锦囊管理列表 */
    getList() {
      this.loading = true;
      // this.queryParams.type = '1'
      listJnRole(this.queryParams).then(response => {
        this.roleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取所有模型配置
    getAllLllmConfigs() {
      alllistLllmConfig().then(response => {
        this.ttsOptions = response.data.filter(item => item.configType === 'tts');
        this.sttOptions = response.data.filter(item => item.configType === 'stt');
      });
    },
    // 获取所有类型选项
    getAllTypes() {
      alllistType().then(response => {
        this.typeOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        roleId: null,
        roleName: null,
        roleDesc: null,
        info: null,
        volume: null,
        speech: null,
        emoji: null,
        image: null,
        video: null,
        rollingWords: null,
        welcomeWords: null,
        documentType: [], // 修改为数组
        avatar: null,
        ttsId: null,
        modelId: null,
        sttId: null,
        vadSpeechTh: null,
        vadSilenceTh: null,
        vadEnergyTh: null,
        vadSilenceMs: null,
        voiceName: null,
        state: null,
        isDefault: null,
        userId: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加锦囊管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const roleId = row.roleId || this.ids
      getJnRole(roleId).then(response => {
        this.form = response.data;
        // 将逗号分隔的字符串转换为数组
        if (this.form.documentType) {
          this.documentTypeList = this.form.documentType.split(',');
        } else {
          this.documentTypeList = [];
        }
        if (this.form.volume) {
          this.form.volume = Number(this.form.volume);
        }
        this.open = true;
        this.title = "查看锦囊管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 创建提交数据的副本
          const submitData = {...this.form};
          // 将数组转换为逗号分隔的字符串
          if (Array.isArray(submitData.knowledgeType)) {
            submitData.knowledgeType = submitData.knowledgeType.join(',');
          }
          if (this.form.documentType) {
            submitData.documentType = this.documentTypeList.join(',');
          }

          if (this.form.roleId != null) {
            updateJnRole(submitData).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addJnRole(submitData).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roleIds = row.roleId || this.ids;
      this.$modal.confirm('是否确认删除锦囊管理编号为"' + roleIds + '"的数据项？').then(function () {
        return delJnRole(roleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/role/export', {
        ...this.queryParams
      }, `role_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
